//导入 db.js
const db = require("./db/db");
const mongoose = require('mongoose');
//导入模型对象
const BookModel = require('./models/BookModel');

db(
    () => {
        //插入文档
        BookModel.create(
            {
                name: "三体",
                author: "刘慈欣",
                price: 29.9,
            },
            (err, data) => {
                //如果插入错误, 则抛出错误, 停止运行
                if (err) throw err;
                //打印 data 数据.    data 是插入成功后的文档对象
                console.log(data);

                //8. 关闭链接 『选做』
                mongoose.connection.close();
            }
        );
    },
    //连接失败的回调
    () => {
        console.log("连接失败");
    }
);
